package com.hotel.project.demo.controller;

import com.hotel.project.common.api.CommonResult;
import com.hotel.project.demo.api.HotelResultCode;
import com.hotel.project.demo.domain.HotelAnswerEntity;
import com.hotel.project.demo.domain.HotelQuestionEntity;
import com.hotel.project.demo.domain.ResponseBean;
import com.hotel.project.demo.service.HotelQuestionService;
import com.hotel.project.demo.service.impl.HotelAnswerServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/hotel/question")
public class HotelQuestionController extends BaseController<HotelQuestionService, HotelQuestionEntity>{

    @Autowired
    private HotelAnswerServiceImpl hotelAnswerServiceImpl;

    @GetMapping("/list-by-hotel-id")
    public CommonResult<List<Map<String,Object>>> listHotelQuestionEntityByHotelId(@RequestParam("id") Long id) {
        List<HotelQuestionEntity> hotelQuestionEntityList = service.listHotelQuestionEntityByHotelId(id);
        if (hotelQuestionEntityList != null && hotelQuestionEntityList.size() > 0){
            Map<String,Object> map = new HashMap<>();
            List<Map<String,Object>> list = new ArrayList<>();
            hotelQuestionEntityList.forEach(hotelQuestionEntity -> {
                map.clear();
                map.put("questionId", hotelQuestionEntity.getId());
                map.put("questionContent",hotelQuestionEntity.getContent());
                List<HotelAnswerEntity> answerList = hotelAnswerServiceImpl.listHotelAnswerEntityByQuestionId(hotelQuestionEntity.getId());
                map.put("answerList",answerList);
                list.add(map);
            });
            return CommonResult.success(list,"获取问题成功");
        }

        return CommonResult.failed(HotelResultCode.FAILED,"获取问题失败");
    }

    @GetMapping("/list-by-user-id")
    public CommonResult<List<Map<String,Object>>> listHotelQuestionEntityByUserId(@RequestParam("id") Long userId) {
        List<HotelQuestionEntity> hotelQuestionEntityList = service.listHotelQuestionEntityByUserId(userId);
        if (hotelQuestionEntityList != null && hotelQuestionEntityList.size() > 0){
            Map<String,Object> map = new HashMap<>();
            List<Map<String,Object>> list = new ArrayList<>();
            hotelQuestionEntityList.forEach(hotelQuestionEntity -> {
                map.clear();
                map.put("questionId", hotelQuestionEntity.getId());
                map.put("questionContent",hotelQuestionEntity.getContent());
                List<HotelAnswerEntity> answerList = hotelAnswerServiceImpl.listHotelAnswerEntityByQuestionId(hotelQuestionEntity.getId());
                map.put("answerList",answerList);
                list.add(map);
            });
            return CommonResult.success(list,"获取问题成功");
        }

        return CommonResult.failed(HotelResultCode.FAILED,"获取问题失败");
    }

    @GetMapping("/list-by-ask-user-id")
    public CommonResult<List<Map<String,Object>>> listHotelQuestionEntityByAskUserId(@RequestParam("id") Long id,@RequestParam("status") Integer status) {
        List<HotelQuestionEntity> hotelQuestionEntityList = service.listHotelQuestionEntityByAskUserId(id,status);
        if (hotelQuestionEntityList != null && hotelQuestionEntityList.size() > 0){
            Map<String,Object> map = new HashMap<>();
            List<Map<String,Object>> list = new ArrayList<>();
            hotelQuestionEntityList.forEach(hotelQuestionEntity -> {
                map.clear();
                map.put("questionId", hotelQuestionEntity.getId());
                map.put("questionContent",hotelQuestionEntity.getContent());
                List<HotelAnswerEntity> answerList = hotelAnswerServiceImpl.listHotelAnswerEntityByQuestionId(hotelQuestionEntity.getId());
                map.put("answerList",answerList);
                list.add(map);
            });
            return CommonResult.success(list,"获取问题成功");
        }

        return CommonResult.failed(HotelResultCode.FAILED,"获取问题失败");
    }

    @DeleteMapping("/save")
    @ApiOperation(value = "添加问大家", notes = "添加问大家")
    public ResponseBean saveQuestion(HotelQuestionEntity entity) throws Exception{
        ResponseBean rb = new ResponseBean();
        try{
            service.saveOrUpdate(entity);
            rb.setData(entity);
            rb.setCode(10000L);
            rb.setMessage("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            rb.setMessage("添加失败");
        }
        return rb;
    }


}
